package com.fsy.Controller;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fsy.entities.Roles;
import com.fsy.entities.Students;
import com.fsy.entities.Userchecks;
import com.fsy.service.StudentsService;
import com.fsy.service.UserchecksService;
import com.fsy.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/userchecks")
public class UserChecksController {
    @Resource
    private UserchecksService userchecksService;
    @Resource
    private StudentsService studentsService;

    @GetMapping("/getTodayChecks")
    public Map<String,Object> getTodayChecks(Integer userId){
        //判断当前用户是不是咨询师
        List<Roles> rolesByUserId = studentsService.getRolesByUserId(userId);
        for (Roles r: rolesByUserId){
            if (!r.getName().equals("咨询师")){
                return Result.toClient("-1","非咨询师不能签到");
            }
        }
        Userchecks today = userchecksService.getTodaySign(userId);
        if (today == null){
            return Result.toClient("1","未签到");
        }
        if (today.getCheckState() == 1){
            return Result.toClient("2","已签到，未签退");
        }
        if (today.getCheckState() == 2){
            return Result.toClient("3","已签退");
        }
        return Result.toClient("0","数据异常");
    }
    /**
     * 签到
     * @param userId
     * @return
     */
    @PostMapping("signIn")
    public Map<String,Object> getTodaySign(Integer userId,String userName){
        Userchecks uc = new Userchecks();
        uc.setUserId(userId);
        uc.setUserName(userName);
        uc.setCheckState(1);
        uc.setCheckInTime(new Date());
        Boolean rel = userchecksService.save(uc);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","签到成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","签到失败");
        }
        return rslmap;
    }

    /**
     * 签退
     * @param userId
     * @return
     */
    @PostMapping("signOut")
    public Map<String,Object> singOut(Integer userId){
        //10分钟之内不能签退
        Userchecks today = userchecksService.getTodaySign(userId);
        Date signDate = today.getCheckInTime();
        long nowtime = new Date().getTime();
        long signtime10 = signDate.getTime()+6000L;
        if (signtime10>nowtime){
            return  Result.toClient("1","签到10分钟后才能签退哦");
        }
        //update userchecks set checkState='2',checkOutTime=now() where userId = 12 and checkTime > CURDATE
        UpdateWrapper<Userchecks> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("checkState","2").set("checkOutTime",new Date())
                .eq("userId",userId).gt("checkInTime", DateUtils.format(new Date(),"yyyy-MM-dd"));
        Boolean rel = userchecksService.update(updateWrapper);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","签退成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","签退失败");
        }
        return rslmap;
    }


    /**
     * 查询全部签到情况
     * @param userchecks
     * @return
     */
    @GetMapping("/selectUserChecks")
    public Map<String,Object> selectUserChecks(Userchecks userchecks){
        List<Userchecks> userchecksList = userchecksService.selectAllUserChecks(userchecks);
        int counts = userchecksService.getCountUserchecks(userchecks);
        return Result.toClient("0","成功",counts,userchecksList);
    }

    /**
     * 咨询经理强制退出
     * @param id
     * @return
     */
    @PostMapping("/qzsignOut")
    public Map<String,Object> qzsignOut(Integer id){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dqsj = df.format(new Date());
        UpdateWrapper<Userchecks> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("checkOutTime", dqsj)
                     .set("checkState", 2)
                     .eq("id", id);
        return userchecksService.update(updateWrapper) ? Result.toClient("0", "签退成功") : Result.toClient("1", "签退失败");

    }
}
